using System;
using System.IO;
using System.Text;
using System.Linq;
using SZipHandler.Entities;

namespace SZipHandler
{
    public class Logger
    {
        private readonly string _logPath;
        private readonly string[] _logingStatus;

        public Logger(string logPath, string[] logingStatus)
        {
            _logPath = logPath;
            _logingStatus = logingStatus;
        }

        public void Log(LogStatus logStatus, string message)
        {
            try
            {
                if(_logingStatus.Count(x => x == logStatus.ToString()) <= 0)
                    return;

                var logName = Path.Combine(_logPath, DateTime.Now.ToString("yyyyMMdd") + ".log");

                FileStream fs = null;
                using (fs = File.Exists(logName) ? new FileStream(logName, FileMode.Append, FileAccess.Write) : new FileStream(logName, FileMode.CreateNew, FileAccess.Write))
                {
                    var line = string.Format("[{0}] [{1}] [{2}]\n", DateTime.Now.ToString(), logStatus, message);
                    Console.WriteLine(line);

                    fs.Write(Encoding.Unicode.GetBytes(line), 0, Encoding.Unicode.GetByteCount(line));

                    fs.Flush();
                    fs.Close();
                }
            }
            catch
            {
            }
        }

        public void Log(Exception exception)
        {
            Log(LogStatus.ERROR, exception.ToString());
        }
    }
}